package it.gat.webservice.resource.impl;

import java.sql.SQLException;

import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;

import it.gat.dao.ArticoloDAO;
import it.gat.model.Articolo;
import it.gat.util.dao.DAOFactory;
import it.gat.webservice.utils.Impostazioni;

public class ArticoloResource extends ServerResource{
	
	@Post
	public void insert(Articolo articolo) {
		DAOFactory daoFactory = DAOFactory.getDAOFactory(Integer.parseInt(Impostazioni.getProperty("DB_TYPE")));
		ArticoloDAO articoloDAO = daoFactory.getArticoloDAO();
		try {
			articoloDAO.insert(articolo);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Get
	public Articolo retriveByCodice() {
		DAOFactory daoFactory = DAOFactory.getDAOFactory(Integer.parseInt(Impostazioni.getProperty("DB_TYPE")));
		ArticoloDAO articoloDAO = daoFactory.getArticoloDAO();
		Articolo articolo;
		try {
			articolo = articoloDAO.retriveByCodice(getRequestAttributes().get("codice").toString());
		} catch (NumberFormatException e) {
			return null;
		} catch (SQLException e) {
			return null;
		}
		return articolo;
	}

	@Put
	public void update(Articolo articolo) {
		DAOFactory daoFactory = DAOFactory.getDAOFactory(Integer.parseInt(Impostazioni.getProperty("DB_TYPE")));
		ArticoloDAO articoloDAO = daoFactory.getArticoloDAO();
		try {
			articoloDAO.update(articolo);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Delete
	public void delete(Articolo articolo) {
		DAOFactory daoFactory = DAOFactory.getDAOFactory(Integer.parseInt(Impostazioni.getProperty("DB_TYPE")));
		ArticoloDAO articoloDAO = daoFactory.getArticoloDAO();
		try {
			articoloDAO.delete(getRequestAttributes().get("codice").toString());
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}
